package com.cbn.ibcp.web.smp.utils;

import com.cbn.ibcp.common.exception.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.regex.Pattern;

public class ParamValidator {
    private static Pattern MOBILE_PHONE = Pattern.compile("1\\d{10}");

    public static void assertAfterNow(LocalDate localDate, String errorMessage) {
        if (localDate == null || !localDate.isAfter(LocalDate.now())) {
            throw new BusinessException((errorMessage));
        }
    }
    public static void assertAfterNow(LocalDateTime localDateTime, String errorMessage) {
        if (localDateTime == null || !localDateTime.isAfter(LocalDateTime.now())) {
            throw new BusinessException((errorMessage));
        }
    }
    public static void assertAfterNow(LocalTime localTime, String errorMessage) {
        if (localTime == null || !localTime.isAfter(LocalTime.now())) {
            throw new BusinessException((errorMessage));
        }
    }
    public static void assertNotNull(Object obj, String errorMessage) {
        if (obj == null) {
            throw new BusinessException((errorMessage));
        }
    }
    public static void assertTrue(boolean b, String errorMessage) {
        if (!b) {
            throw new BusinessException(errorMessage);
        }
    }
    public static void assertFalse(boolean b, String errorMessage) {
        if (b) {
            throw new BusinessException(errorMessage);
        }
    }
    public static void assertNotEmpty(String str, String errorMessage) {
        if (StringUtils.isBlank(str)) {
            throw new BusinessException(errorMessage);
        }
    }
    public static void assertNotEmpty(List<?> coll, String errorMessage) {
        if (CollectionUtils.isEmpty(coll)) {
            throw new BusinessException(errorMessage);
        }
    }
    public static void assertEmpty(List<?> coll, String errorMessage) {
        if (CollectionUtils.isNotEmpty(coll)) {
            throw new BusinessException(errorMessage);
        }
    }
    public static void assertNotPositive(Integer l, String errorMessage) {
        if (l != null && l > 0) {
            throw new BusinessException(errorMessage);
        }
    }

    public static void assertNotNullAndPositive(Long l, String errorMessage) {
        if (l == null || l <= 0) {
            throw new BusinessException(errorMessage);
        }
    }

    public static void assertNotNullAndPositive(Integer l, String errorMessage) {
        if (l == null || l < 0) {
            throw new BusinessException(errorMessage);
        }
    }

    public static void assertNotNullAndNonNegative(Integer l, String errorMessage) {
        if (l == null || l < 0) {
            throw new BusinessException(errorMessage);
        }
    }

    public static void assertNotNullAndNonNegative(BigDecimal l, String errorMessage) {
        if (l == null || l.signum() < 0) {
            throw new BusinessException(errorMessage);
        }
    }

    public static void assertNotNullAndPositiveInteger(BigDecimal decimal, String errorMessage) {
        if (decimal==null || decimal.signum() <= 0 || decimal.multiply(BigDecimal.valueOf(10)).floatValue() % 10 > 0) {
            throw new BusinessException(errorMessage);
        }
    }

    public static void assertMobilePhone(String phone, String errorMessage) {
        if (StringUtils.isNotEmpty(phone)) {
            if (!MOBILE_PHONE.matcher(phone).matches()) {
                throw new BusinessException(errorMessage);
            }
        }
    }
}
